package cn.itrip.service;

import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.mapper.BaseQuery;
import cn.itrip.vo.ItripHotelVO;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @ClassName HotelSearchServiceImpl
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2020/12/23 10:07
 * @Version 1.0
 **/
@Service
public class HotelSearchServiceImpl implements  HotelSearchService {

    @Resource
    private BaseQuery<ItripHotelVO> baseQuery;



    @Override
    public Page<ItripHotelVO> getHotelByPage(SearchHotelVO searchHotelVO) {
        //创建一个SolrQuery
        SolrQuery query =new SolrQuery("*:*");

        //solrquery封装q
        StringBuilder builder =new StringBuilder("");
        if(EmptyUtils.isNotEmpty(searchHotelVO.getDestination()))
        builder.append("destination:"+searchHotelVO.getDestination());
        if(EmptyUtils.isNotEmpty(searchHotelVO.getKeywords())){
            searchHotelVO.setKeywords(searchHotelVO.getKeywords().replace(" ","|"));
            builder.append(" And keyword:"+searchHotelVO.getKeywords());
        }
        query.setQuery(builder.toString());
        //solrquery封装fq
          //商圈条件（a or b or c）
        if(EmptyUtils.isNotEmpty(searchHotelVO.getTradeAreaIds())){
            //123,334,324
            //(tradingAreaIdS:*,123,* OR tradingAreaIdS:*,334,* OR tradingAreaIdS:*,324,* )
            StringBuilder builder1 = new StringBuilder("(");
            String[] ids=searchHotelVO.getTradeAreaIds().split(",");
            boolean isFirst=true;
            for(String id:ids){
                if(isFirst){
                    builder1.append("tradingAreaIds:*,"+id+",*");
                    isFirst=false;
                }else{
                    builder1.append(" OR tradingAreaIds:*,"+id+",*");
                }
            }
            builder1.append(")");
            query.addFilterQuery(builder1.toString());
        }

        //价格  最大价格10000
         if(EmptyUtils.isNotEmpty(searchHotelVO.getMaxPrice())){
            query.addFilterQuery(" minPrice:[* TO "+searchHotelVO.getMaxPrice()+"]");
        }
        //最小价格
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMinPrice())){
            query.addFilterQuery("maxPrice:["+searchHotelVO.getMinPrice()+" TO *]");
        }

        //星级
        if(EmptyUtils.isNotEmpty(searchHotelVO.getHotelLevel())){
            query.addFilterQuery("hotelLevel:"+searchHotelVO.getHotelLevel());
        }

        //特色
            //123.334.234
            //(featureIds:*,123,* OR featureIds:*,334,* OR featureIds:*,234,* )
        if(EmptyUtils.isNotEmpty(searchHotelVO.getFeatureIds())){
            //123,334,324
            //(tradingAreaIdS:*,123,* OR tradingAreaIdS:*,334,* OR tradingAreaIdS:*,324,* )
            StringBuilder builder1 = new StringBuilder("(");
            String[] ids=searchHotelVO.getFeatureIds().split(",");
            boolean isFirst=true;
            for(String id:ids){
                if(isFirst){
                    builder1.append("featureIds:*,"+id+",*");
                    isFirst=false;
                }else{
                    builder1.append(" OR featureIds:*,"+id+",*");
                }
            }
            builder1.append(")");
            query.addFilterQuery(builder1.toString());
        }

        //排序
        if(EmptyUtils.isNotEmpty(searchHotelVO.getAscSort())){
            query.addSort(searchHotelVO.getAscSort(),SolrQuery.ORDER.asc);
        }
        if(EmptyUtils.isNotEmpty(searchHotelVO.getDescSort())){
            query.addSort(searchHotelVO.getDescSort(),SolrQuery.ORDER.desc);
        }

        //执行basequery分页查询  //返回数据
        return baseQuery.getHotelByPage(query,searchHotelVO.getPageNo(),searchHotelVO.getPageSize(),ItripHotelVO.class);
    }
}
